<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="应用代码" prop="appCode">
              <el-input v-model="queryParams.appCode" placeholder="请输入应用代码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用名称" prop="appName">
              <el-input v-model="queryParams.appName" placeholder="请输入应用名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用登陆图标" prop="appLoginlogourl">
              <el-input v-model="queryParams.appLoginlogourl" placeholder="请输入应用登陆图标" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="登陆显示图" prop="appLoginpic">
              <el-input v-model="queryParams.appLoginpic" placeholder="请输入登陆显示图" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="登陆背景图1" prop="appLoginbgpic1">
              <el-input v-model="queryParams.appLoginbgpic1" placeholder="请输入登陆背景图1" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="登陆背景图2" prop="appLoginbgpic2">
              <el-input v-model="queryParams.appLoginbgpic2" placeholder="请输入登陆背景图2" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="登陆背景图3" prop="appLoginbgpic3">
              <el-input v-model="queryParams.appLoginbgpic3" placeholder="请输入登陆背景图3" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="登陆背景图4" prop="appLoginbgpic4">
              <el-input v-model="queryParams.appLoginbgpic4" placeholder="请输入登陆背景图4" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="登陆背景图5" prop="appLoginbgpic5">
              <el-input v-model="queryParams.appLoginbgpic5" placeholder="请输入登陆背景图5" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用图标" prop="appLogourl">
              <el-input v-model="queryParams.appLogourl" placeholder="请输入应用图标" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用图标ico" prop="appLogoicourl">
              <el-input v-model="queryParams.appLogoicourl" placeholder="请输入应用图标ico" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用版权" prop="copyright">
              <el-input v-model="queryParams.copyright" placeholder="请输入应用版权" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用域名" prop="domain">
              <el-input v-model="queryParams.domain" placeholder="请输入应用域名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="备案号" prop="beian">
              <el-input v-model="queryParams.beian" placeholder="请输入备案号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用描述" prop="description">
              <el-input v-model="queryParams.description" placeholder="请输入应用描述" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="首页" prop="indexpage">
              <el-input v-model="queryParams.indexpage" placeholder="请输入首页" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="主界面" prop="mainpage">
              <el-input v-model="queryParams.mainpage" placeholder="请输入主界面" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="关键词" prop="keywords">
              <el-input v-model="queryParams.keywords" placeholder="请输入关键词" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="搜索关键字" prop="searchkeywords">
              <el-input v-model="queryParams.searchkeywords" placeholder="请输入搜索关键字" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="帮助URL" prop="helpurl">
              <el-input v-model="queryParams.helpurl" placeholder="请输入帮助URL" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="会话URL" prop="pushrul">
              <el-input v-model="queryParams.pushrul" placeholder="请输入会话URL" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用树标志" prop="appTypecode">
              <el-input v-model="queryParams.appTypecode" placeholder="请输入应用树标志" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="应用层级" prop="appLevel">
              <el-input v-model="queryParams.appLevel" placeholder="请输入应用层级" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:app:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['system:app:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['system:app:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:app:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="appList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="应用ID" align="center" prop="appId" v-if="true" />
        <el-table-column label="应用代码" align="center" prop="appCode" />
        <el-table-column label="应用名称" align="center" prop="appName" :show-overflow-tooltip="true" />
        <el-table-column label="应用登陆图标" align="center" prop="appLoginlogourl" />
        <el-table-column label="登陆显示图" align="center" prop="appLoginpic" />
        <el-table-column label="登陆背景图1" align="center" prop="appLoginbgpic1" />
        <el-table-column label="登陆背景图2" align="center" prop="appLoginbgpic2" />
        <el-table-column label="登陆背景图3" align="center" prop="appLoginbgpic3" />
        <el-table-column label="登陆背景图4" align="center" prop="appLoginbgpic4" />
        <el-table-column label="登陆背景图5" align="center" prop="appLoginbgpic5" />
        <el-table-column label="应用图标" align="center" prop="appLogourl" />
        <el-table-column label="应用图标ico" align="center" prop="appLogoicourl" />
        <el-table-column label="应用版权" align="center" prop="copyright" :show-overflow-tooltip="true" />
        <el-table-column label="应用域名" align="center" prop="domain" :show-overflow-tooltip="true" />
        <el-table-column label="备案号" align="center" prop="beian" :show-overflow-tooltip="true" />
        <el-table-column label="应用描述" align="center" prop="description" :show-overflow-tooltip="true" />
        <el-table-column label="首页" align="center" prop="indexpage" :show-overflow-tooltip="true" />
        <el-table-column label="主界面" align="center" prop="mainpage" :show-overflow-tooltip="true" />
        <el-table-column label="关键词" align="center" prop="keywords" />
        <el-table-column label="搜索关键字" align="center" prop="searchkeywords" :show-overflow-tooltip="true" />
        <el-table-column label="帮助URL" align="center" prop="helpurl" :show-overflow-tooltip="true" />
        <el-table-column label="会话URL" align="center" prop="pushrul" :show-overflow-tooltip="true" />
        <el-table-column label="应用树标志" align="center" prop="appTypecode" />
        <el-table-column label="应用层级" align="center" prop="appLevel" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:app:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:app:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改应用对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="800px" append-to-body>
      <el-form ref="appFormRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用代码" prop="appCode">
              <el-input v-model="form.appCode" placeholder="请输入应用代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用名称" prop="appName">
              <el-input v-model="form.appName" placeholder="请输入应用名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用登陆图标" prop="appLoginlogourl">
              <el-input v-model="form.appLoginlogourl" placeholder="请输入应用登陆图标" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登陆显示图" prop="appLoginpic">
              <el-input v-model="form.appLoginpic" placeholder="请输入应用登陆显示图片" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登陆背景图1" prop="appLoginbgpic1">
              <el-input v-model="form.appLoginbgpic1" placeholder="请输入应用登陆背景图片1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登陆背景图2" prop="appLoginbgpic2">
              <el-input v-model="form.appLoginbgpic2" placeholder="请输入应用登陆背景图片2" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登陆背景图3" prop="appLoginbgpic3">
              <el-input v-model="form.appLoginbgpic3" placeholder="请输入应用登陆背景图片3" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登陆背景图4" prop="appLoginbgpic4">
              <el-input v-model="form.appLoginbgpic4" placeholder="请输入应用登陆背景图片4" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登陆背景图5" prop="appLoginbgpic5">
              <el-input v-model="form.appLoginbgpic5" placeholder="请输入应用登陆背景图片5" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用图标" prop="appLogourl">
              <el-input v-model="form.appLogourl" placeholder="请输入应用图标" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用图标ico" prop="appLogoicourl">
              <el-input v-model="form.appLogoicourl" placeholder="请输入应用图标ico" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用版权" prop="copyright">
              <el-input v-model="form.copyright" placeholder="请输入应用版权" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用域名" prop="domain">
              <el-input v-model="form.domain" placeholder="请输入应用域名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备案号" prop="beian">
              <el-input v-model="form.beian" placeholder="请输入备案号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用描述" prop="description">
              <el-input v-model="form.description" placeholder="请输入应用描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首页" prop="indexpage">
              <el-input v-model="form.indexpage" placeholder="请输入主界面中的首页" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主界面" prop="mainpage">
              <el-input v-model="form.mainpage" placeholder="请输入登陆后主界面" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关键词" prop="keywords">
              <el-input v-model="form.keywords" placeholder="请输入关键词" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="搜索关键字" prop="searchkeywords">
              <el-input v-model="form.searchkeywords" placeholder="请输入搜索关键字" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="帮助URL" prop="helpurl">
              <el-input v-model="form.helpurl" placeholder="请输入帮助URL" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会话URL" prop="pushrul">
              <el-input v-model="form.pushrul" placeholder="请输入会话URL" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用标志" prop="appTypecode">
              <el-input v-model="form.appTypecode" placeholder="请输入同一应用树标志" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用层级" prop="appLevel">
              <el-input v-model="form.appLevel" placeholder="请输入同一应用树中应用的层级" />
            </el-form-item>
          </el-col>
          <el-col :span="12"> </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="App" lang="ts">
import { listApp, getApp, delApp, addApp, updateApp } from '@/api/system/app';
import { AppVO, AppQuery, AppForm } from '@/api/system/app/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const appList = ref<AppVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const appFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: AppForm = {
  appId: undefined,
  appCode: undefined,
  appName: undefined,
  appLoginlogourl: undefined,
  appLoginpic: undefined,
  appLoginbgpic1: undefined,
  appLoginbgpic2: undefined,
  appLoginbgpic3: undefined,
  appLoginbgpic4: undefined,
  appLoginbgpic5: undefined,
  appLogourl: undefined,
  appLogoicourl: undefined,
  copyright: undefined,
  domain: undefined,
  beian: undefined,
  description: undefined,
  indexpage: undefined,
  mainpage: undefined,
  keywords: undefined,
  searchkeywords: undefined,
  helpurl: undefined,
  pushrul: undefined,
  appTypecode: undefined,
  appLevel: undefined,
}
const data = reactive<PageData<AppForm, AppQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    appCode: undefined,
    appName: undefined,
    appLoginlogourl: undefined,
    appLoginpic: undefined,
    appLoginbgpic1: undefined,
    appLoginbgpic2: undefined,
    appLoginbgpic3: undefined,
    appLoginbgpic4: undefined,
    appLoginbgpic5: undefined,
    appLogourl: undefined,
    appLogoicourl: undefined,
    copyright: undefined,
    domain: undefined,
    beian: undefined,
    description: undefined,
    indexpage: undefined,
    mainpage: undefined,
    keywords: undefined,
    searchkeywords: undefined,
    helpurl: undefined,
    pushrul: undefined,
    appTypecode: undefined,
    appLevel: undefined,
    params: {
    }
  },
  rules: {
    appId: [
      { required: true, message: "应用ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询应用列表 */
const getList = async () => {
  loading.value = true;
  const res = await listApp(queryParams.value);
  appList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  appFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: AppVO[]) => {
  ids.value = selection.map(item => item.appId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加应用";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: AppVO) => {
  reset();
  const _appId = row?.appId || ids.value[0]
  const res = await getApp(_appId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改应用";
}

/** 提交按钮 */
const submitForm = () => {
  appFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.appId) {
        await updateApp(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addApp(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: AppVO) => {
  const _appIds = row?.appId || ids.value;
  await proxy?.$modal.confirm('是否确认删除应用编号为"' + _appIds + '"的数据项？').finally(() => loading.value = false);
  await delApp(_appIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/app/export', {
    ...queryParams.value
  }, `app_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
